﻿using System.IO;

namespace Z.IconLibraryGenerator.WinForm
{
    public partial class IconLibraryGeneratorManager
    {
        public static void GenerateProjectFile(ProjectConfiguration project)
        {
            bool isLegacy = project.Framework == ProjectFrameworkType.Framework20;
            DirectoryInfo dir = My.TemplateFile.ProjectFile;
            var dirOutput = Path.Combine(project.OutputDirectory, project.ProjectName);

            var iconEnum = GetEnumText(project);
            var css = GetCssText(project);
            var embedded = GetEmbeddedText(project);
            var projectembedded = GetProjectEmbeddedText(project);
            var includeExtensions = GetIncludeExtensionsText(project);
            string frameworkVersion = isLegacy ? "v2.0" : "v3.5";
            string namespaceStart = isLegacy ? "namespace " + project.ProjectNamespace + "{" : "";
            string namespaceEnd = isLegacy ? "}" : "";
            string CssFileName = project.ProjectName + ".css";
            string HandlerPath = project.ProjectNamespace + ".axd";
            string Guid = System.Guid.NewGuid().ToString();
            string sourceControl = @"
    <SccProjectName>SAK</SccProjectName>
    <SccLocalPath>SAK</SccLocalPath>
    <SccAuxPath>SAK</SccAuxPath>
    <SccProvider>SAK</SccProvider>
";

            foreach (FileInfo file in dir.GetFiles("*.*", SearchOption.AllDirectories))
            {
                string template = file.ReadToEnd();

                template = template.Replace("@(Model.SourceControl)", sourceControl);
                template = template.Replace("@(Model.Namespace)", project.ProjectNamespace);
                template = template.Replace("@(Model.HandlerPath)", HandlerPath);
                template = template.Replace("@(Model.CssFileName)", CssFileName);
                template = template.Replace("@(Model.IncludeExtensions)", includeExtensions);
                template = template.Replace("@(Model.CssFileNameDebug)", CssFileName.Replace(".css", ".debug.css"));
                template = template.Replace("@(Model.IconEnum)", iconEnum);
                template = template.Replace("@(Model.Css)", css);
                template = template.Replace("@(Model.Embedded)", embedded);
                template = template.Replace("@(Model.ProjectEmbedded)", projectembedded);
                template = template.Replace("@(Model.FrameworkVersion)", frameworkVersion);
                template = template.Replace("@(Model.NamespaceStart)", namespaceStart);
                template = template.Replace("@(Model.NamespaceEnd)", namespaceEnd);
                template = template.Replace("@(Model.Guid)", Guid);
                template = template.Replace("@(Model.ExtensionMethod)", isLegacy ? "" : "this ");

                string newFileName = file.FullName.Replace("@(Model.Namespace)", project.ProjectNamespace);
                newFileName = newFileName.Replace("@(Model.CssFileName)", CssFileName);
                newFileName = newFileName.Replace("@(Model.ProjectName)", project.ProjectName);

                FileInfo output = newFileName.Replace(dir.FullName, dirOutput).ToFileInfo();
                output = Path.Combine(output.Directory.FullName, output.GetFileNameWithoutExtension()).ToFileInfo();
                Directory.CreateDirectory(output.Directory.FullName);
                template.SaveAs(output);
            }
        }
    }
}